/*
 * 【BAE扩展服务：mongodb】
 */

var Db = require('mongodb').Db;
var Server = require('mongodb').Server;
var config = require('config');

var db_name = config.get('mongodb.db'); //数据库名称
var db_host = config.get('mongodb.host'); //数据库地址
var db_port = config.get('mongodb.port'); // 数据库端口
var username = config.get('credentials.ak'); //用户AK
var password = config.get('credentials.sk'); //用户SK

var db = new Db(db_name, new Server(db_host, db_port, {}), {
    w: 1
});

// 启动时建立连接
db.open(function(err, db) {
    db.authenticate(username, password, function(err, res) {
        if (err) {
            db.close();
            console.error('mongodb authenticate failed!');
        } else {
            console.log("open mongodb");
        }
    });
});

exports.db = db;